package storage.v1;

import com.onpositive.knowledge.model.IThing;
import com.onpositive.mediawiki.propertyparser.ParsedDocument;
import com.onpositive.mediawiki.propertyparser.PropertyCharacteristic;
import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.units.ParsedProperty;

public class WikiStorageProperty implements IProperty {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7388522489483516323L;
	protected PropertyCharacteristic pChar;
	private IWikiStorage storage;
	private WikiStoragePropertyMeta meta;

	public WikiStorageProperty( PropertyCharacteristic pChar, IWikiStorage storage) {
		super();
		this.pChar = pChar;
		this.storage = storage;
		meta = new WikiStoragePropertyMeta( pChar );
	}

	@Override
	public IMeta getMeta() {

		return meta;
	}

	@Override
	public String getId() {

		return pChar.getName();
	}

	@Override
	public Object getValue(Object obj) {
		if (obj instanceof IThing) {
			obj = ((IThing) obj).getId();
		}
		if (obj instanceof String) {
			String id = (String) obj;
			ParsedDocument parsedDocument = storage.getParsedDocument(id);
			if (parsedDocument != null) {
				ParsedProperty property = parsedDocument.getProperty(pChar
						.getName());
				if (property != null) {
					return property.getValues();
				}
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return getId();
	}
}
